#
# amarokSyndication - Amarok script for syndicating (atom file format) your music to Google Pages
#
# $Id: amarokSyndication.pl 2008-05-10 19:15:46Z dpino $
#
# (C) 2007,2008 Diego Pino  <pinowsky at gmail com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

##
# Provides functions for fecthing images from Google Product Search (a.k.a froogle)
#

# require("$ENV{'HOME'}/.kde/share/apps/amarok/scripts/atomSyndication/prefs/preferences.pl");

use LWP::UserAgent;
use URI::Escape;
use Data::Dumper;

##
# Parses a GPS page result searchin for first image on list and returns its URL
#
sub parseURL()
{
	local(@strings) = @_;

	foreach (@strings) {
		# print IMAGE "string: $_";
		if (/name=\"ImageURL\" value=\"http:\/\/base.googlehosted.com\/base_media\?q=(.*)\"><input type=hidden name=\"SearchQuery\"/) {
			# print IMAGE "First parsing: $1\n";
			$linematch = $1;
			if ($linematch =~ /(.*\.jpg|gif|png)/gi) {
				# print IMAGE "Second parsing: $1\n";
				return $1;
			}
		}
	}

	return "";
}

##
# Queries Google Product Search by artist and album
#
sub fetchUrlAlbumCover()
{
	local($artist, $album) = @_;

	$url = "http://www.google.com/products?q=" . uri_escape("$artist $album");

	# print IMAGE "URL : " . Dumper($url);
	my $ua = LWP::UserAgent->new();
	local($response) = $ua->get($url);

	@strings = split(/"\n"/,$response->content);

	return (&parseURL(@strings));
}

1;
